package com.ruoyi.manager.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.manager.domain.ArvinClientVersion;
import com.ruoyi.manager.service.IArvinClientVersionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户端版本信息Controller
 * 
 * @author ruoyi
 * @date 2023-04-09
 */
@RestController
@RequestMapping("/client/version")
public class ArvinClientVersionController extends BaseController
{
    @Autowired
    private IArvinClientVersionService arvinClientVersionService;

    /**
     * 查询客户端版本信息列表
     */
    @PreAuthorize("@ss.hasPermi('client:version:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArvinClientVersion arvinClientVersion)
    {
        startPage();
        List<ArvinClientVersion> list = arvinClientVersionService.selectArvinClientVersionList(arvinClientVersion);
        return getDataTable(list);
    }

    /**
     * 导出客户端版本信息列表
     */
//    @PreAuthorize("@ss.hasPermi('client:version:export')")
    @Log(title = "客户端版本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArvinClientVersion arvinClientVersion)
    {
        List<ArvinClientVersion> list = arvinClientVersionService.selectArvinClientVersionList(arvinClientVersion);
        ExcelUtil<ArvinClientVersion> util = new ExcelUtil<ArvinClientVersion>(ArvinClientVersion.class);
        util.exportExcel(response, list, "客户端版本信息数据");
    }

    /**
     * 获取客户端版本信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('client:version:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(arvinClientVersionService.selectArvinClientVersionById(id));
    }

    /**
     * 新增客户端版本信息
     */
//    @PreAuthorize("@ss.hasPermi('client:version:add')")
    @Log(title = "客户端版本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArvinClientVersion arvinClientVersion)
    {
        return toAjax(arvinClientVersionService.insertArvinClientVersion(arvinClientVersion));
    }

    /**
     * 修改客户端版本信息
     */
//    @PreAuthorize("@ss.hasPermi('client:version:edit')")
    @Log(title = "客户端版本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArvinClientVersion arvinClientVersion)
    {
        return toAjax(arvinClientVersionService.updateArvinClientVersion(arvinClientVersion));
    }

    /**
     * 删除客户端版本信息
     */
//    @PreAuthorize("@ss.hasPermi('client:version:remove')")
    @Log(title = "客户端版本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(arvinClientVersionService.deleteArvinClientVersionByIds(ids));
    }
    
    /**
     * 客户端检查更新接口 - 无需登录权限
     */
    @Anonymous
    @GetMapping("/check")
    public AjaxResult checkUpdate(String currentVersion)
    {
        // 获取最新版本信息
        ArvinClientVersion latestVersion = arvinClientVersionService.selectLatestVersion();
        
        if (latestVersion == null) {
            return AjaxResult.error("未找到版本信息");
        }
        
        // 如果没有传入当前版本，或版本不同，则返回最新版本信息
        if (currentVersion == null || !currentVersion.equals(latestVersion.getVersion())) {
            return AjaxResult.success("发现新版本", latestVersion);
        } else {
            return AjaxResult.success("当前已是最新版本");
        }
    }
    
    /**
     * 获取最新版本信息 - 无需登录权限
     */
    @Anonymous
    @GetMapping("/latest")
    public AjaxResult getLatestVersion()
    {
        ArvinClientVersion latestVersion = arvinClientVersionService.selectLatestVersion();
        if (latestVersion != null) {
            return AjaxResult.success(latestVersion);
        } else {
            return AjaxResult.error("未找到版本信息");
        }
    }
} 